<?php
/**
 * SMS系统控制层基类，所有的Action类都需要继承该类
 *
 * @copyright   Copyright 2011, Richie
 * @author      Richie <richbossman@163.com>
 * @package     SMS
 * @link        http://www.letphp.com
 * 
 * $Id: SMSAction.class.php 63 2011-04-02 15:05:34Z 442767452@qq.com $
 */
abstract class SMSAction extends Action {
	//dataGrid参数
	public $datagrid = array();
	
	//初始化方法
	protected function _initialize(){
		//系统设置
		$this->setConfig();
		//是否是ajax提交，ajax提交情况下调用简单参数
		$is_ajax = $this->isAjax();
		if($is_ajax){
			$this->setdateGrid();
		}else{
			//页面标题
			$this->assign('page_title', L('TITLE_'.APP_NAME.'_'.MODULE_NAME.'_'.ACTION_NAME));
			//顶部按钮
			$this->setButton();
			//当前模块JS
			$js = strtolower(WEB_PUBLIC_PATH.'/js/'.APP_NAME.'/'.MODULE_NAME.'.js');
			if(is_file(ROOT_PATH.$js)){
				$this->assign('action_js', $js);
			}
		}
	}
	
	public function _before__initialize(){
		
	}
	
	/**
	 * 设置系统配置
	 *
	 */
	public function setConfig(){
		$path = C('SYSTEM_CONFIG_PATH');
		if(is_file($path)) return C(include $path);
		$config = M()->getConfig();
		$text = '<?php '."\n".'if(!defined("ROOT_PATH")) exit();'."\n";
		$text .= 'return unserialize(\''.serialize($config).'\');';
		$text .= "\n".'?>';
		@file_put_contents($path, $text);
		return C(include $path);
	}
	
	//检查权限
	public function checkPurview(){
		//超级管理员拥有所有权限
		if(isset($_SESSION['administartor']) && $_SESSION['administartor'] == true){
			return true;
		}
		$purview = isset($_SESSION['_ACCESS_LIST']) ? $_SESSION['_ACCESS_LIST'] : array();
		if(empty($purview)){
			$this->error('您没有权限访问该模块！');
		}
		if(isset($purview)){
			
		}
		
	}
	
	//设置按钮
	public function setButton(){
		$button = '_button_'.MODULE_NAME.'_'.ACTION_NAME;
		if(method_exists($this, $button)){
			$buttons = call_user_func(array(&$this, $button));
			if(empty($buttons)) return;
			foreach ($buttons as $key=>$btn){
				$btns = array();
				if(!empty($btn) && !is_array($btn)){
					$btns['action']= $btn;
				}else{
					$btns = $btn;
				}
				if(!isset($btns['action'])){
					unset($buttons[$key]); //未设置action，该按钮无效
					continue;
				}
				$btns['app'] = isset($btns['app']) ? $btns['app'] : APP_NAME;
				$btns['module'] = isset($btns['module']) ? $btns['module'] : MODULE_NAME;
				$btns['href'] = isset($btns['href']) ? $btns['href'] : U($btns['app'].'://'.$btns['module'].'/'.$btns['action']);
				$btns['icon'] = isset($btns['icon']) ? $btns['icon'] : $btns['action'];
				$btns['text'] = isset($btns['text']) ? $btns['text'] : L('BUTTON_'.$btns['module'].'_'.$btns['action']);
				//URL参数
				if(isset($btns['params'])){
					if(is_string($btns['params'])){
						$btns['href'] .= '&'.$btns['params'];
					}elseif(is_array($btns['params'])){
						foreach ($btns['params'] as $keys=>$param){
							$btns['href'] .= '&'.$keys.'='.$param;
						}
					}
				}
				//根据权限删除按钮
				/* ... */
				$buttons[$key]=$btns;
				unset($btns, $btn, $key);
			}
			$this->assign('button', $buttons);
		}
		return;
	}
	
	//默认页
	public function index(){
		Cookie::set ( '_currentUrl_', __SELF__ );
		$this->display();
	}
	
	//新增页面
	public function add(){
		//初始化一些常用的变量
		$vo = array('sort'=>'50');
		$this->assign('vo', $vo);
		$this->display();
	}
	
	//新增处理
	public function insert(){
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$list=$model->add ();
		if ($list!==false) {
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('OPT_INERT_OK'));
		} else {
			//失败提示
			$this->error (L('OPT_INERT_FAIL'));
		}
	}
	
	//修改页面
	public function edit(){
		$name = $this->getActionName();
		$model = M ( $name );
		$pk = $model->getPk ();
		$id = isset($_REQUEST [$pk]) ? intval($_REQUEST[$pk]) : 0;
		if(empty($id)){
			$this->error(L('VAR_PK_ERROR'));
		}
		$getpk = 'getBy'.$pk;
		$vo = $model->$getpk ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	
	//修改处理
	public function update(){
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			//成功提示
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('OPT_UPDATE_OK'));
		} else {
			//错误提示
			$this->error (L('OPT_UPDATE_FAIL'));
		}
	}
	
	//显示
	public function read(){
		$this->display();
	}
	
	//详情页面
	public function detail(){
		$this->read();
	}
	
	//删除操作
	public function delete(){
		
	}
	
	public function remove(){
		
	}
	
	//设置DataGrid参数
	public function setdateGrid(){
		$this->datagrid['page'] = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
		$this->datagrid['rows'] = isset($_REQUEST['rows']) ? intval($_REQUEST['rows']) : 10;
		$this->datagrid['sort'] = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : 'sort';
		$this->datagrid['order'] = isset($_REQUEST['order']) ? $_REQUEST['order'] : 'ASC';
	}
	
	
}
?>